package com.cyl.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.wms.domain.Platform;
import com.cyl.wms.domain.Platform;
import com.cyl.wms.mapper.PlatformMapper;
import com.cyl.wms.pojo.query.AddrQuery;
import com.cyl.wms.pojo.query.PlatformQuery;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * 地址码Service业务层处理
 *
 * @author Lisi
 */
@Service
public class PlatformService {
    @Autowired
    private PlatformMapper platformMapper;

    /**
     * 查询货区
     *
     * @param id 货区主键
     * @return 货区
     */
    public Platform selectById(Long id) {
        return platformMapper.selectById(id);
    }

    /**
     * 查询货区列表
     *
     * @param query 查询条件
     * @param page  分页条件
     * @return 货区
     */
    public List<Platform> selectList(PlatformQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<Platform> qw = new QueryWrapper<>();
        qw.eq("del_flag", 0);
        String addrNo = query.getPlatformNo();
        if (!StringUtils.isEmpty(addrNo)) {
            qw.eq("platform_no", addrNo);
        }
        String addrNameLike = query.getPlatformNameLike();
        if (!StringUtils.isEmpty(addrNameLike)) {
            qw.like("area_name", addrNameLike);
        }
        Long warehouseId = query.getWarehouseId();
        if (warehouseId != null) {
            qw.eq("warehouse_id", warehouseId);
        }
        return platformMapper.selectList(qw);
    }

    /**
     * 新增货区
     *
     * @param Platform 货区
     * @return 结果
     */
    public int insert(Platform Platform) {
        Platform.setDelFlag(0);
        Platform.setCreateTime(LocalDateTime.now());
        return platformMapper.insert(Platform);
    }

    /**
     * 修改货区
     *
     * @param area 货区
     * @return 结果
     */
    public int update(Platform area) {
        return platformMapper.updateById(area);
    }

    /**
     * 批量删除货区
     *
     * @param ids 需要删除的货区主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        return platformMapper.updateDelFlagByIds(ids);
    }

    /**
     * 删除货区信息
     *
     * @param id 货区主键
     * @return 结果
     */
    public int deleteById(Long id) {
        Long[] ids = {id};
        return platformMapper.updateDelFlagByIds(ids);
    }

    public List<Platform> selectByIdIn(Collection<Long> ids) {
        QueryWrapper<Platform> qw = new QueryWrapper<>();
        qw.in("id", ids);
        return platformMapper.selectList(qw);
    }
}
